<?php

namespace app\modules\m\controllers;

use yii\web\Controller;
use \app\modules\m\controllers\common\BaseController;
use \app\models\Member\Member;
use \app\models\Member\MemberFav;

use \app\models\Member\MemberCart;
use \app\models\Member\MemberComments;
use \app\models\pay\PayOrder;
use \app\models\book\Book;
use \app\models\user\China;
use \app\models\user\MemberAddress;

use \app\models\sms\SmsCaptcha;
use \app\common\services\UtilService;
use \app\common\services\UrlService;
class UserController extends BaseController
{
    public function actionIndex()
    {
        // var_dump($this->current_user);
        return $this->render('index',['member'=>$this->current_user]);
    }
    public function actionAddress()
    {
        $list=MemberAddress::find()->where(['member_id'=>$this->current_user->id])->asArray()->all();
        // var_dump($list);die;
        //地址
        $arr=[];
        foreach($list as $k=>$v){
            $tmp_province=china::find()->select('name')->where(['id'=>$v['province_id']])->one();
            $province=$tmp_province['name'];
            $tmp_city=china::find()->select('name')->where(['id'=>$v['city_id']])->one();
            $city=$tmp_city['name'];
            $tmp_area=china::find()->select('name')->where(['id'=>$v['area_id']])->one();
            $area=$tmp_area['name'];
            $arr[$k]['nickname']=$v['nickname'];
            $arr[$k]['mobile']=$v['mobile'];
            $arr[$k]['id']=$v['id'];
            $arr[$k]['address']=$province.$city.$area.$v['address'];
        }
        // var_dump($arr);die;
        return $this->render('address',['list'=>$arr]);
    }
    public function actionAddress_set()
    {
        if(\Yii::$app->request->isPost){
            //保存地址
            $province_id=$this->post('province_id','');
            $city_id=$this->post('city_id','');
            $area_id=$this->post('area_id',0);
            $nickname=$this->post('nickname','');
            $mobile=$this->post('mobile','');
            $address=$this->post('address','');
            $act=$this->post('act','');
            $address_id=$this->post('address_id','');
            $id=$this->post('id','');
            //如果为删除
            if($act=='del'){
                $info=MemberAddress::find()->where(['id'=>$address_id])->one();
                $res=$info->delete();
                if($res){
                    return $this->renderJson([],"删除成功");
                }
            }
            //如果为设为默认
            if($act=='set_default'){
                //将其他的设为0
                MemberAddress::updateAll(['is_default'=>0],'member_id='.$this->current_user->id);
                $info=MemberAddress::find()->where(['id'=>$address_id])->one();
                $info->is_default=1;
                $res=$info->save(0);
                if($res){
                    return $this->renderJson([],"设置成功");
                }
            }
            //如果为添加或者更新
            if($address && $mobile && $nickname && $act=='add'){
                if(!$province_id){
                    return $this->renderJson([],"请选择省",-1);
                }
                if(!$city_id){
                    return $this->renderJson([],"请选择市",-1);
                }
                $is_find=MemberAddress::find()->where(['id'=>$id])->one();
                if($is_find){
                    $model_address=$is_find;
                }else{
                    $model_address=new MemberAddress();
                    $model_address->member_id=$this->current_user->id;
                     $model_address->created_time=time();
                     $model_address->is_default=0;
                }
                $model_address->nickname=$nickname;
                $model_address->mobile=$mobile;
                $model_address->address=$address;
                $model_address->province_id=$province_id;
                $model_address->city_id=$city_id;
                $model_address->area_id=$area_id;
                $model_address->status=1;
                $model_address->updated_time=time();
                $res=$model_address->save(0);
                if(!$res){
                    return $this->renderJson([],"操作失败",-1);
                }
                return $this->renderJson([],"操作成功");
            }
            //省市区联动
            $id=$this->post('id','');
            $city_id=$this->post('city_id',"");
            if($id){
                //查找市
                $list=China::find()->where(['pid'=>$id])->asArray()->all();
                if(!$list){
                    return $this->renderJson([],"查找市区失败~",-1);
                }
            }
            if($city_id){
                //查找县
                $list=China::find()->where(['pid'=>$city_id])->asArray()->all();
                if(!$list){
                    return $this->renderJson([],"查找县失败~",-1);
                }
            }
            $list=json_encode($list);
            return $this->renderJson(['list'=>$list]);
        }
        $list=China::find()->where(['pid'=>0])->asArray()->all();
        $id=$this->get('address_id','');
        $info=MemberAddress::find()->where(['id'=>$id])->one();
        if(!$info){
            $info='';
        }
        return $this->render('address_set',['list'=>$list,'info'=>$info]);
    }
    //查看用户收藏
    public function actionFav()
    {
        
        if(\Yii::$app->request->isPost){
            $id=$this->post("id","");
            // echo $id;
            $res=MemberFav::find()->where(['id'=>$id])->one();
            if(!$res){
                return $this->renderJson([],"非法操作",-1);
            }
            $res->delete();
            return $this->renderJson([],"删除成功");
        }
        $member=$this->current_user;
        $list=MemberFav::find()->join('left join','book','member_fav.book_id=book.id')->select('book.name,book.price,book.id as book_id,book.main_image,member_fav.id')->where(['member_id'=>$member->id])->asArray()->all();
        
        return $this->render('fav',['list'=>$list]);
    }
    public function actionBind()
    {
    	
        if(\Yii::$app->request->isGet){
             return $this->render('bind');
        }
        $mobile=$this->post('mobile',"");
        $mobile_code=$this->post('mobile_code',"");//短信验证码
        if(!$mobile_code){
            return $this->renderJson([],"请输入短信验证码",-1);
        }
        //判断手机号是否已经绑定
        $is_find=Member::find()->where(['mobile'=>$mobile])->one();
        if($is_find){
            //测试
            $cookie = new \yii\web\Cookie();
            $cookie -> name = 'member';        //cookie的名称
            $cookie -> expire = time() + 3600;     //存活的时间
            $cookie -> httpOnly = true;        //无法通过js读取cookie
            $cookie -> value =$is_find;      //cookie的值
            \Yii::$app->response->getCookies()->add($cookie);
            return $this->renderJson([],"手机号已经绑定,请直接手机登录",-1);
        }
        //判断输入的短信验证码是否正确
        $is_find=SmsCaptcha::find()->where(['mobile'=>$mobile,'captcha'=>$mobile_code])->one();
        if(!$is_find){
            return $this->renderJson([],"短信验证码输入错误",-1);
        }
        $new_member=new Member();
        $new_member->nickname='';
        $new_member->mobile=$mobile;
        $new_member->sex=1;
        $new_member->reg_ip=UtilService::getIp();
        $new_member->status=1;
        $new_member->created_time=time();
        $new_member->updated_time=time();
        $res=$new_member->save(0);
        if(!$res){
            return $this->renderJson([],"操作非法",-1);
        }
        $cookies = \Yii::$app->response->cookies;
        $cookies->add(new \yii\web\Cookie([
            'name' => 'member',
            'value' => $new_member,
            'expire'=>time()+3600,
        ]));
        return $this->renderJson([],"绑定成功");
        
    }
    public function actionCart()
    {
        if(\Yii::$app->request->isPost){
            $member=$this->current_user;
            $book_id=$this->post("book_id","");
            $quantity=$this->post("quantity","");

            $is_find=MemberCart::find()->where(['member_id'=>$member->id,'book_id'=>$book_id])->one();
            if(!$is_find){
                return $this->renderJson([],"非法操作~",-1);
            }
            $is_find->quantity=$quantity;
            $is_find->updated_time=time();
            $is_find->save(0);
            return $this->renderJson([],"操作成功"); 
        }
    	$member=$this->current_user;
        $list=MemberCart::find()->join('inner join','book','member_cart.book_id=book.id')->select('book.id,book.name,book.price,book.main_image,book.stock,member_cart.quantity')->where(['member_id'=>$member->id])->orderBy(['member_cart.id'=>SORT_DESC])->asArray()->all();
        
        return $this->render('cart',['list'=>$list]);
    }
    public function actionOrder()
    {
        
        // var_dump($this->current_user);die;
    	// $list=PayOrder::find()->where(['member_id'=>$member_id])->all();
        $list = PayOrder::find()->join('inner join','pay_order_item','pay_order_item.pay_order_id=pay_order.id')->join('inner join','book','book.id=pay_order_item.target_id')->select('book.main_image,book.price,book.name,pay_order.status,pay_order.order_sn,pay_order.created_time,pay_order_item.pay_order_id,pay_order_item.quantity,pay_order_item.target_id,pay_order.express_info')->where(['<>','pay_order.status',0])->andWhere(['pay_order.member_id'=>$this->current_user->id])->orderBy(['pay_order.id'=>SORT_DESC])->asArray()->all();
        // var_dump($list);
        // die;
        return $this->render('order',['list'=>$list]);
    }
    //评论列表
    public function actionComment()
    {
        $member_id=$this->current_user->id;
        $list=MemberComments::find()->where(['member_id'=>$member_id])->all();
        return $this->render('comment',['list'=>$list]);
    }
    //发表评论
    public function actionComment_set()
    {
        if(\Yii::$app->request->isPost){
            $score=$this->post("score","");
            $book_id=$this->post("book_id","");
            $pay_order_id=$this->post("pay_order_id","");
            $comment=$this->post("comment","");
            $member_id=$this->current_user->id;
            //判断是否已经评论
            $is_find=MemberComments::find()->where(['member_id'=>$member_id,'book_id'=>$book_id])->one();
            if($is_find){
                return $this->renderJson([],"你已经评论过啦~");
            }
            $model_member_comment=new MemberComments();
            $model_member_comment->score=$score;
            $model_member_comment->book_id=$book_id;
            $model_member_comment->member_id=$member_id;
            
            $model_member_comment->pay_order_id=$pay_order_id;
            $model_member_comment->content=$comment;
            $model_member_comment->created_time=time();
            $res=$model_member_comment->save(0);
            if(!$res){
                return $this->renderJson([],"评论失败",-1);
            }
            $book=Book::find()->where(['id'=>$book_id])->one();
            $book->comment_count+=1;
            $book->updated_time=time();
            $book->save(0);
            $payorder=PayOrder::find()->where(['id'=>$pay_order_id])->one();
            $payorder->comment_status=1;
            $payorder->updated_time=time();
            $payorder->save(0);
            
            return $this->renderJson([],"评论成功");
        }
        $pay_order_id=$this->get('pay_order_id',"");
        $book_id=PayOrder::find()->join('inner join','pay_order_item','pay_order.id=pay_order_item.pay_order_id')->select('target_id as book_id')->asArray()->one();

        return $this->render('comment_set',['pay_order_id'=>$pay_order_id,'book_id'=>$book_id['book_id']]);
    }
}
